<template>
	<view
		class="goodItemInner"
        :style="{marginBottom:isLastGoodItem ? '0' : '40rpx'}"
		>
		<image
			mode="aspectFill"
			:src="goodEle.image"
			class="goodItem_image"
			@tap="showGoodDetailModal(goodEle)"></image>
		<view class="good_right">
			<text class="name">{{ goodEle.storeName }}</text>
			<text class="tips">{{ goodEle.storeInfo }}</text>
			<view class="price_and_action">
				<text class="priceBox">
					<text class="yuan">￥</text>
					<text class="relPrice">{{ goodEle.price }}</text>
				</text>
				<view class="btn-group" v-if="goodEle.stock > 0">
					<button
						type="primary"
						class="btn property_btn"
						hover-class="none"
						size="mini"
						@tap="showGoodDetailModal(goodEle)">
						选规格
					</button>
					<view class="dot" v-show="goodCartNum(goodEle.id)">
						{{ goodCartNum(goodEle.id) }}
					</view>
				</view>

				<view class="emptyClass" v-if="goodEle.stock == 0">已售罄</view>
			</view>
		</view>
	</view>
</template>
<script setup>
	import { ref, computed, nextTick, onMounted, watch,getCurrentInstance } from 'vue'
	import productDetailModal from './productDetailModal.vue'
    const props = defineProps({
        cart:{
            type: Array,
            default: () => []
        },
        goodEle:{
            type: Object,
            default: () => {}
        },
        isLastGoodItem:{
            type: Boolean,
            default: false
        },
		goodIndex:{
            type: Number,
            default: 0
		}
    })
	const { proxy } = getCurrentInstance()
	function getNavHeaderHeight() {
		return new Promise(resolve => {
			const query = uni.createSelectorQuery().in(proxy)
			query.select('.goodItem_image')
				.boundingClientRect(data => {
					if (data) {
						let obj = {
							left:data.left + (data.width / 2),
							top: data.top + (data.height / 2)
						}
						console.log('goodItem_image',obj);
						resolve(obj)
					} else {
						resolve({}) // 没取到就默认 0
					}
				})
				.exec()
		})
	}
    const goodCartNum = computed(() => {
		//计算单个饮品添加到购物车的数量
		return id =>
			props.cart.reduce((acc, cur) => {
				if (cur.id === id) {
					return (acc += cur.number)
				}
				return acc
			}, 0)
	})
	const emit = defineEmits(['showGoodDetail'])
	const showGoodDetailModal = async (newGood) => {
		const res = await getNavHeaderHeight()
		let obj = {
			...res,
			goodIndex: props.goodIndex
		}
		emit('showGoodDetail', newGood, obj)
		
	}
</script>
<style lang="scss" scoped>
	.goodItemInner {
		background-color: #fff;
		display: flex;
		align-items: center;
		height: 150rpx;
		.goodItem_image {
			width: 150rpx;
			height: 100%;
			margin-right: 20rpx;
			border-radius: 10rpx;
			overflow: hidden;
			background-color: #f5f5f5;
		}

		.good_right {
			flex: 1;
			height: 100%;
			overflow: hidden;
			display: flex;
			flex-direction: column;
			align-items: flex-start;
			padding-right: 14rpx;

			.name {
				font-size: $font-size-base;
				// margin-bottom: 10rpx;
				width: 100%;
				overflow: hidden;
				color: #000;
				text-overflow: ellipsis;
				white-space: nowrap;
			}

			.tips {
				width: 100%;
				height: 40rpx;
				line-height: 40rpx;
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
				font-size: 22rpx;
				color: $text-color-assist;
				margin-top: 6rpx;
			}

			.price_and_action {
				width: 100%;
				margin-top: auto;
				display: flex;
				justify-content: space-between;
				align-items: flex-end;

				.priceBox {
					.yuan {
						font-size: 22rpx;
					}
					.relPrice {
						font-size: 30rpx;
						font-weight: 600;
					}
				}
				.emptyClass{
					font-size: $font-size-sm;
					background-color: #ccc;
					color: #666;
					padding: 0 20rpx;
					height: 44rpx;
					line-height: 44rpx;
					border-radius: 24rpx;
				}
				.btn-group {
					display: flex;
					justify-content: space-between;
					align-items: center;
					position: relative;

					.btn {
						padding: 0 20rpx;
						height: 44rpx;
						line-height: 44rpx;
						box-sizing: border-box;
						background-color: $qingzhen-btn-primary-color;
						font-size: $font-size-sm;
						

						&.property_btn {
							border-radius: 24rpx;
						}

					}

					.dot {
						position: absolute;
						background-color: #ffffff;
						border: 1px solid $qingzhen-btn-primary-color;
						color: $qingzhen-btn-primary-color;
						font-size: $font-size-sm;
						width: 36rpx;
						height: 36rpx;
						line-height: 36rpx;
						text-align: center;
						border-radius: 100%;
						right: -12rpx;
						top: -10rpx;
					}
				}
			}
		}
	}
</style>
